package com.jiyun.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiyun.entity.day04.Kc;
import com.jiyun.entity.day04.KcDTO1;
import com.jiyun.entity.day08.TrainingDTO;
import com.jiyun.entity.day10.CyDTO;
import com.jiyun.entity.day10.JsDTO;
import com.jiyun.entity.day10.SquadsDTO;
import com.jiyun.feign.PeopleFeign;
import com.jiyun.service.KcService;
import com.jiyun.vo.ResultResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Title: LoginController
 * @Author 杜瑞
 * @Date 2023/8/22 11:17
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private PeopleFeign peopleFeign;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private KcService kcService;

    @RequestMapping("/login1")
    public ResultResponse login(String username,String password){
        try {
            ResultResponse login = peopleFeign.login(username, password);
            if (login.getData()!=null){
                return ResultResponse.success(login);
            }else {
                return ResultResponse.error();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findUserName")
    public ResultResponse findName(){
        try {
            String  username = (String) redisTemplate.opsForValue().get("username");
            return ResultResponse.success(username);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKcNum")
    public ResultResponse findKcNum() {
        try {
            ResultResponse kcNum = peopleFeign.findKcNum();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findJsNum")
    public ResultResponse findJsNum() {
        try {
            ResultResponse kcNum = peopleFeign.findJsNum();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPxpxNumber")
    public ResultResponse findPxpxNumber() {
        try {
            ResultResponse kcNum = peopleFeign.findPxpxNumber();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findCsNum")
    public ResultResponse findCsNum() {
        try {
            ResultResponse kcNum = peopleFeign.findCsNum();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKcName")
    public ResultResponse findKcName() {
        try {
            ResultResponse kcNum = peopleFeign.findKcName();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPxName")
    public ResultResponse findPxName() {
        try {
            ResultResponse kcNum = peopleFeign.findPxName();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findCsName")
    public ResultResponse findCsName() {
        try {
            ResultResponse kcNum = peopleFeign.findCsName();
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKNum1")
    public ResultResponse findKNum1(String name) {
        try {
            ResultResponse kcNum = peopleFeign.findKNum1(name);
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKNum2")
    public ResultResponse findKNum2(String name) {
        try {
            ResultResponse kcNum = peopleFeign.findKNum2(name);
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKNum3")
    public ResultResponse findKNum3(String name) {
        try {
            ResultResponse kcNum = peopleFeign.findKNum3(name);
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findKNum4")
    public ResultResponse findKNum4(String name) {
        try {
            ResultResponse kcNum = peopleFeign.findKNum4(name);
            return ResultResponse.success(kcNum);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("likePost1")
    public  ResultResponse likePost1(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            if (name1==null){
                redisTemplate.opsForValue().set(name,1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum("1");
                kcService.updateById(kc);
            }else {
                Integer sum=(name1 + 1);
                redisTemplate.opsForValue().set(name, name1 + 1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum(String.valueOf(sum));
                kcService.updateById(kc);
            }
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("likePost2")
    public  ResultResponse likePost2(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            if (name1==null){
                redisTemplate.opsForValue().set(name,1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum("1");
                kcService.updateById(kc);
            }else {
                Integer sum=(name1 + 1);
                redisTemplate.opsForValue().set(name, name1 + 1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum(String.valueOf(sum));
                kcService.updateById(kc);
            }
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("likePost3")
    public  ResultResponse likePost3(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            if (name1==null){
                redisTemplate.opsForValue().set(name,1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum("1");
                kcService.updateById(kc);
            }else {
                Integer sum=(name1 + 1);
                redisTemplate.opsForValue().set(name, name1 + 1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum(String.valueOf(sum));
                kcService.updateById(kc);
            }
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("likePost4")
    public  ResultResponse likePost4(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            if (name1==null){
                redisTemplate.opsForValue().set(name,1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum("1");
                kcService.updateById(kc);
            }else {
                Integer sum=(name1 + 1);
                redisTemplate.opsForValue().set(name, name1 + 1);
                QueryWrapper<Kc> wrapper = new QueryWrapper<>();
                wrapper.eq("name",name);
                Kc kc = kcService.getOne(wrapper);
                kc.setZNum(String.valueOf(sum));
                kcService.updateById(kc);
            }
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("find1")
    public ResultResponse find1(String name){
        try {
            Integer o = (Integer) redisTemplate.opsForValue().get(name);
            return ResultResponse.success(o);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("find2")
    public ResultResponse find2(String name){
        try {
            Integer o = (Integer) redisTemplate.opsForValue().get(name);
            return ResultResponse.success(o);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("find3")
    public ResultResponse find3(String name){
        try {
            Integer o = (Integer) redisTemplate.opsForValue().get(name);
            return ResultResponse.success(o);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("find4")
    public ResultResponse find4(String name){
        try {
            Integer o = (Integer) redisTemplate.opsForValue().get(name);
            return ResultResponse.success(o);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPx")
    public ResultResponse findPx(){
        try {
            ResultResponse px = peopleFeign.findPx();
            return ResultResponse.success(px);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPxNum1")
    public ResultResponse findPxNum1(){
        try {
            ResultResponse pxNum1 = peopleFeign.findPxNum1();
            return ResultResponse.success(pxNum1);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPxNum2")
    public ResultResponse findPxNum2(){
        try {
            ResultResponse pxNum1 = peopleFeign.findPxNum2();
            return ResultResponse.success(pxNum1);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findPxNum3")
    public ResultResponse findPxNum3(){
        try {
            ResultResponse pxNum1 = peopleFeign.findPxNum3();
            return ResultResponse.success(pxNum1);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findSj1")
    public ResultResponse findSj1(){
        try {
            ResultResponse sj1 = peopleFeign.findSj1();
            return ResultResponse.success(sj1);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findSj2")
    public ResultResponse findSj2(){
        try {
            ResultResponse sj2 = peopleFeign.findSj2();
            return ResultResponse.success(sj2);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatelikePost1")
    public  ResultResponse updatelikePost1(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            redisTemplate.opsForValue().set(name, name1 -1);
            QueryWrapper<Kc> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Kc kc = kcService.getOne(wrapper);
            kc.setZNum(String.valueOf(name1 -1));
            kcService.updateById(kc);
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatelikePost2")
    public  ResultResponse updatelikePost2(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            redisTemplate.opsForValue().set(name, name1 -1);
            QueryWrapper<Kc> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Kc kc = kcService.getOne(wrapper);
            kc.setZNum(String.valueOf(name1 -1));
            kcService.updateById(kc);
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatelikePost3")
    public  ResultResponse updatelikePost3(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            redisTemplate.opsForValue().set(name, name1 -1);
            QueryWrapper<Kc> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Kc kc = kcService.getOne(wrapper);
            kc.setZNum(String.valueOf(name1 -1));
            kcService.updateById(kc);
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatelikePost4")
    public  ResultResponse updatelikePost4(String name){
        try {
            Integer  name1 = (Integer) redisTemplate.opsForValue().get(name);
            redisTemplate.opsForValue().set(name, name1 -1);
            QueryWrapper<Kc> wrapper = new QueryWrapper<>();
            wrapper.eq("name",name);
            Kc kc = kcService.getOne(wrapper);
            kc.setZNum(String.valueOf(name1 -1));
            kcService.updateById(kc);
            return ResultResponse.success(null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }


    @RequestMapping("findMyKc")
    public ResultResponse findMyKc(@RequestBody KcDTO1 kcDTO1) {
        try {
            kcDTO1.setBq(null);
            ResultResponse list = peopleFeign.findMyKc(kcDTO1);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findMyJs")
    public ResultResponse findMyJs(@RequestBody JsDTO jsDTO) {
        try {
            ResultResponse list = peopleFeign.findMyJs(jsDTO);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }


    @RequestMapping("findMyUser")
    public ResultResponse findMyUser() {
        try {
            ResultResponse list = peopleFeign.findMyUser();
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findMyPx")
    public ResultResponse findMyPx(@RequestBody TrainingDTO trainingDTO) {
        try {
            ResultResponse list = peopleFeign.findMyPx(trainingDTO);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findMyZd")
    public ResultResponse findMyZd(@RequestBody SquadsDTO squadsDTO) {
        try {
            ResultResponse list = peopleFeign.findMyZd(squadsDTO);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("findMyCy")
    public ResultResponse findMyCy(@RequestBody CyDTO cyDTO) {
        try {
            ResultResponse list = peopleFeign.findMyCy(cyDTO);
            return ResultResponse.success(list);
        } catch (Exception e){
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("updatePassword")
    public ResultResponse updatePassword(Integer id,String password){
        try {
            ResultResponse resultResponse = peopleFeign.updatePassword(id, password);
            return ResultResponse.success(resultResponse);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }

    @RequestMapping("chongzhi")
    public ResultResponse chongzhi(Integer id){
        try {
            ResultResponse resultResponse = peopleFeign.chongzhi(id);
            return ResultResponse.success(resultResponse);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultResponse.error();
        }
    }







}

